﻿using System;
using System.Collections.Generic;
using System.Linq;
using Locator.Silverlight.Client.Models.PersonalLocationsServiceReference;
using Locator.Silverlight.Client.Models.ViewModels.Elements;

namespace Locator.Silverlight.Client.Models
{
    public class PersonalLocationModel : ModelBase<IPersonalLocationsServiceAsync>, IPersonalLocationModel
    {
        /// <summary>
        /// Initializes a new instance of the LocatorModel class.
        /// </summary>
        public PersonalLocationModel()
            : this(new PersonalLocationsServiceClient("BasicHttpBinding_IPersonalLocationsService"))
        {

        }

        public PersonalLocationModel(IPersonalLocationsServiceAsync service)
            : base(service)
        {

        }

        public void GetPersonalLocationsForCurrentUserAsync(Action<List<PersonalLocationViewModel>, Exception> callback)
        {
            this.Service.GetPersonalLocationsForCurrentUserCompleted += (source, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var personalLocationViewModelList = new List<PersonalLocationViewModel>();
                    foreach (var personalLocation in eventArgs.Result)
                    {
                        personalLocationViewModelList.Add(new PersonalLocationViewModel(personalLocation));
                    }

                    callback(personalLocationViewModelList, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.GetPersonalLocationsForCurrentUserAsync();
        }

        public void GetPersonalLocationForCurrentUserAsync(int ID, Action<PersonalLocationViewModel, Exception> callback)
        {
            this.Service.GetPersonalLocationForCurrentUserCompleted += (source, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    var personalLocationViewModel = new PersonalLocationViewModel(eventArgs.Result);
                    callback(personalLocationViewModel, null);
                }
                else
                {
                    callback(null, eventArgs.Error);
                }
            };

            this.Service.GetPersonalLocationForCurrentUserAsync(ID);
        }

        public void AddPersonalLocationForCurrentUserAsync(string description, double latitude, double longitude, Action<Exception> callback)
        {
            this.Service.AddPersonalLocationForCurrentUserCompleted += (source, eventArgs) =>
            {
                callback(eventArgs.Error);
            };

            this.Service.AddPersonalLocationForCurrentUserAsync(description, latitude, longitude);
        }

        public void DeletePersonalLocationForCurrentUserAsync(int personalLocationId, Action<Exception> callback)
        {
            this.Service.DeletePersonalLocationForCurrentUserCompleted += (source, eventArgs) =>
            {
                callback(eventArgs.Error);
            };

            this.Service.DeletePersonalLocationForCurrentUserAsync(personalLocationId);
        }


        public void SearchByAddressAsync(string streetAddress, string country, string province, string city, Action<Location, Exception> callback)
        {
            this.Service.SearchByAddressCompleted += (source, eventArgs) =>
            {
                callback(eventArgs.Result, eventArgs.Error);
            };

            this.Service.SearchByAddressAsync(streetAddress, country, province, city);
        }

        public void SearchByPostalCodeAsync(string postalCode, string country, Action<Location, Exception> callback)
        {
            this.Service.SearchByPostalCodeCompleted += (source, eventArgs) =>
            {
                callback(eventArgs.Result, eventArgs.Error);
            };

            this.Service.SearchByPostalCodeAsync(postalCode, country);
        }
    }
}
